/**
 * @Author: LiMeng
 * @Date: 2017/4/11
 */
$(function () {
    //初始化组织结构树
    $('#department_tree').tree({
        url: ROOT_PATH + '/permission/department/getDepartmentTree.shtml?parentId=0',
        lines: true,
        onSelect: function (node) {
            //重新按部门加载datagrid
            $('#dg1').datagrid('load', {
                deptCode: node.attributes.code
            });
        },
        onLoadSuccess: function (node, data) {
            var deptId = data[0].id;
            var deptCode = data[0].attributes.code;
            var isMulti = $.getQueryParam('isMulti');
            var singleSelect = true;
            if (isMulti && isMulti.toString() === '1') {
                singleSelect = false;
            }
            //初始化加载datagrid
            $('#dg1').datagrid({
                method: 'post',
                url: ROOT_PATH + '/permission/user/list.shtml',
                queryParams: {
                    sort: 'addTime',
                    order: 'desc',
                    deptCode: deptCode
                },
                toolbar: '#tb1',
                idField: 'id',
                fitColumns: true,
                pagination: true,
                pageSize: 10,
                rownumbers: true,
                sortOrder: true,
                striped: true,
                fit: true,
                singleSelect: singleSelect,
                columns: [[
                    {field: 'id', width: 80, checkbox: true},
                    {field: 'code', title: '用户编号', width: 100, sortable: true},
                    {field: 'name', title: '用户名', width: 80, sortable: true},
                    {field: 'realName', title: '姓名', width: 80, sortable: true},
                    {
                        field: 'deptName',
                        title: '所属部门',
                        width: 80,
                        // sortable: true,
                        formatter: function (value, row, index) {
                            var name = '';
                            if (row.userDepartmentList && row.userDepartmentList.length > 0) {
                                for (var index in row.userDepartmentList) {
                                    if (row.userDepartmentList[index].deptName != null) {
                                        name += ',' + row.userDepartmentList[index].deptName;
                                    }
                                }
                                return name.substring(1);
                            } else {
                                return '';
                            }
                        }
                    }
                ]]
            });
        }
    });
    $('#btn_clear').on("click", function () {
        $('#tb1').form('reset');
    });
    $('#btn_search').on("click", function () {
        $('#dg1').datagrid('load', $('#tb1').find(':input').serializeJson());
    });
    $('#btn_save').on("click", function () {
        var $selected = $('#dg1').datagrid('getSelections');
        if ($selected.length === 0) {
            layer.alert("请选择一条数据！");
            return;
        }
        WindowManager.closeLayer({
            returnValue: $selected
        });
    });
});